package com.leador.service;

import android.content.ComponentName;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.os.Message;
import android.os.Messenger;
import android.os.RemoteException;

import com.leador.manger.LBSTraceClient;

/**
 * Created by liuha on 2016/9/11.
 */
public class MyServiceConnection implements ServiceConnection {
    LBSTraceClient lbsTraceClient;
    public MyServiceConnection(LBSTraceClient lbsTraceClient) {
        this.lbsTraceClient = lbsTraceClient;
    }
    @Override
    public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
        lbsTraceClient.setMessage(new Messenger(iBinder));
        if(lbsTraceClient.getMessage() == null){
            return;
        }
        try {
            Message message = Message.obtain(null,11);
            message.replyTo = lbsTraceClient.getHMessage();
            lbsTraceClient.getMessage().send(message);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onServiceDisconnected(ComponentName componentName) {
        lbsTraceClient.setMessage(null);
    }
}
